package cc.flyflow.web.controller.process;

import cc.flyflow.biz.process.service.IProcessInstanceRecordService;
import cc.flyflow.biz.process.service.IProcessInstanceService;
import cc.flyflow.biz.process.service.IProcessInstanceUserCopyService;
import cc.flyflow.biz.vo.ProcessDataQueryVO;
import cc.flyflow.biz.vo.ProcessInstanceCopyVo;
import cc.flyflow.biz.vo.ProcessInstanceRecordVO;
import cc.flyflow.common.dto.PageResultDto;
import cc.flyflow.common.dto.R;
import cc.flyflow.common.dto.process.ProcessInstanceDto;
import cc.flyflow.common.dto.process.ProcessInstanceParamDto;
import cc.flyflow.common.dto.process.TaskDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 流程实例接口
 *
 * @author Jyl
 */
@RestController
@RequestMapping("process-instance")
public class ProcessInstanceController
{

    @Resource
    private IProcessInstanceService processInstanceService;
    @Resource
    private IProcessInstanceUserCopyService processInstanceUserCopyService;
    @Resource
    private IProcessInstanceRecordService processInstanceRecordService;

    /**
     * 获取流程详情
     *
     * @param processInstanceParamDto
     * @return
     */
    @GetMapping("getByProcessInstanceId")
    public R getByProcessInstanceId(ProcessInstanceParamDto processInstanceParamDto)
    {
        return R.success(processInstanceRecordService.getByProcessInstanceId(processInstanceParamDto.getProcessInstanceId()));
    }

    /**
     * 停止流程
     * TODO 停止流程
     *
     * @param processInstanceParamDto
     * @return
     */
    @SneakyThrows
    @PostMapping("stopProcessInstance")
    public R stopProcessInstance(@RequestBody ProcessInstanceParamDto processInstanceParamDto)
    {
        return processInstanceService.stopProcessInstance(processInstanceParamDto);
    }

    /**
     * 启动流程
     * TODO 页面启动流程
     *
     * @param processInstanceParamDto
     * @return
     */
    @SneakyThrows
    @PostMapping("startProcessInstance")
    public R startProcessInstance(@RequestBody ProcessInstanceParamDto processInstanceParamDto)
    {
        return processInstanceService.startProcessInstance(processInstanceParamDto);
    }

    /**
     * 终止流程
     * TODO 页面终止流程
     *
     * @param processInstanceParamDto
     * @return
     */
    @SneakyThrows
    @PostMapping("endProcessInstance")
    public R endProcessInstance(@RequestBody ProcessInstanceParamDto processInstanceParamDto)
    {
        return processInstanceService.processEndEvent(processInstanceParamDto);
    }

    /**
     * 查询当前登录用户已办任务的流程实例
     *
     * @param pageDto
     * @return
     */
    @SneakyThrows
    @PostMapping("queryMineDoneProcessInstance")
    public R<PageResultDto<ProcessInstanceDto>> queryMineDoneProcessInstance(@RequestBody ProcessDataQueryVO pageDto)
    {
        return processInstanceService.queryMineDoneProcessInstance(pageDto);
    }

    /**
     * 发起的流程列表
     *
     * @param pageDto
     * @return
     */
    @PostMapping("queryList")
    public R<Page<ProcessInstanceRecordVO>> queryList(@RequestBody ProcessDataQueryVO pageDto)
    {
        return processInstanceService.queryList(pageDto);
    }

    /**
     * 查询流程实例详情
     *
     * @param processInstanceId
     * @return
     */
    @GetMapping("queryDetailByProcessInstanceId")
    public R<ProcessInstanceRecordVO> queryDetailByProcessInstanceId(String processInstanceId)
    {
        return processInstanceService.queryDetailByProcessInstanceId(processInstanceId);
    }

    /**
     * 查询抄送我的流程实例
     *
     * @param pageDto
     * @return
     */
    @SneakyThrows
    @PostMapping("queryMineCCProcessInstance")
    public R<Page<ProcessInstanceCopyVo>> queryMineCCProcessInstance(@RequestBody ProcessDataQueryVO pageDto)
    {
        return processInstanceUserCopyService.queryMineCCProcessInstance(pageDto);
    }

    /**
     * 查询进行中的任务
     *
     * @param processInstanceId
     * @return
     */
    @SneakyThrows
    @PostMapping("queryTaskListInProgress/{processInstanceId}")
    public R<List<TaskDto>> queryTaskListInProgress(@PathVariable String processInstanceId)
    {
        return processInstanceService.queryTaskListInProgress(processInstanceId);
    }
}
